package com.answer.controller.system;

import com.answer.dto.ReturnError;
import com.answer.enums.ReturnInfoEnum;
import com.answer.exception.ElementEmptyException;
import com.answer.exception.RecordEmptyException;
import com.answer.model.Question;
import com.answer.service.QuestionService;
import com.answer.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/13.
 */
@RestController
@RequestMapping(value = "/question")
public class QuestionController {
    private static final Logger log = LoggerFactory.getLogger(QuestionController.class);
    @Autowired
    private QuestionService questionService;

    /**
     * 题目列表
     *
     * @param key
     * @param pageIndex
     * @param limit
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/listQues", method = RequestMethod.POST)
    public ReturnError listQues(@RequestParam(name = "key", required = false) String key,
                                @RequestParam(name = "pageIndex", required = true, defaultValue = "0") Integer pageIndex,
                                @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit,
                                HttpServletRequest request, HttpServletResponse response) {
        List<Question> questionList = new ArrayList<Question>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if (StringUtil.isNotEmpty(key)) {
                map.put("subject", key);
            }
            map.put("delFlag", 1);
            PageHelper.startPage(pageIndex + 1, limit);
            questionList = questionService.listByPage(map);
            PageInfo<Question> p = new PageInfo<Question>(questionList);
            return new ReturnError(false, null, p.getTotal(), p.getList());
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getInfo(), 0L, null);
        }
    }

    /**
     * 新增题目
     *
     * @param ques
     * @return
     */
    @RequestMapping(value = "/saveQues", method = RequestMethod.POST)
    public ReturnError saveQues(Question ques) {
        try {
            return questionService.saveQues(ques);
        } catch (ElementEmptyException e1) {
            log.info(ExceptionUtils.getStackTrace(e1));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e1.getMessage(), null);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    /**
     * 编辑题目
     *
     * @param ques
     * @return
     */
    @RequestMapping(value = "/updateQues", method = RequestMethod.POST)
    public ReturnError updateQues(Question ques) {
        try {
            return questionService.updateQues(ques);
        } catch (ElementEmptyException e1) {
            log.info(ExceptionUtils.getStackTrace(e1));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e1.getMessage(), null);
        } catch (RecordEmptyException e2) {
            log.info(ExceptionUtils.getStackTrace(e2));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e2.getMessage(), null);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    /**
     * 删除题目
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ReturnError remove(String ids) {
        try {
            return questionService.removes(ids);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }
}
